package worddemo;

import jdk.nashorn.api.scripting.ScriptObjectMirror;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**  * Java调用并执行js文件，传递参数，并活动返回值  *   * @author manjushri  */

public class ScriptEngineTest {

    public static void main(String[] args) throws Exception {
    }

    public static String encryptPassword(String qq,String pwd,String verifycode){
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("javascript");

        String jsFileName = "/Users/bianjie/gitproject/worddemo/src/main/java/com/yhuso/worddemo/encrypt.js";   // 读取js文件

        FileReader reader = null;   // 执行指定脚本
        try {
            reader = new FileReader(jsFileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            engine.eval(reader);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        //window.$pt.Encryption
        ScriptObjectMirror o = (ScriptObjectMirror)engine.get("window");
        ScriptObjectMirror g = (ScriptObjectMirror)o.get("$pt");
        ScriptObjectMirror t = (ScriptObjectMirror) g.get("Encryption");

        String n = pwd;
        String salt = qq;//在js中将qq转化为16进制的salt
        Object zz = t.callMember("getEncryption",n,salt,verifycode,null);
        System.out.println("pwd="+zz);
//        if(engine instanceof Invocable) {
//            Invocable invoke = (Invocable)engine;    // 调用merge方法，并传入两个参数
//
//            // c = merge(2, 3);
//
//
//            //i.p = $.Encryption.getEncryption(n, pt.plogin.salt, i.verifycode, pt.plogin.armSafeEdit.isSafe)
//
//            //            String c = (String)invoke.invokeFunction("$.Encryption.getEncryption", n, salt, verifycode);
//            Object c = invoke.invokeMethod(t,"getEncryption", n, salt, verifycode, null);
//            //            Object c = engine.eval("var n='ZWXZ7r3uBuDEVgi'; var salt=4;var verifycode='!VOK'"
//            //                    + "$.Encryption.getEncryption(n, salt, verifycode, undifined);");
//
//
//
//            //            System.out.println("c = " + c);
//        }

        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return zz.toString();
    }

    public static String str2HexStr(String origin) {
        byte[] bytes = origin.getBytes();
        String hex = bytesToHexString(bytes);
        return hex;
    }


    private static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }


}
